package com.lvyuely.demo.pojo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Page<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	private int length = 20,// 页面元素长度
			current = 1,// 当前页
			pages = -1,// 分页总页数
			first = 0;// 分页起始位置
	private long rows = -1L; // 分页总条数
	
	//add start by liguolong 2014-04-30
	
	//显示的页码数
	private int displayPageCount = 7;
	//显示的第一个页码
	private int firstPageNum;
	//显示的最后一个页码 
	private int lastPageNum;
	
	//add end 

	private List<T> dataList = new ArrayList<T>(); // 分页数据列表

	private transient String searchHql;
	private transient String countHql;
	private transient Map<String,Object> params =  new HashMap<String,Object>();

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public int getCurrent() {
		return current;
	}

	public void setCurrent(int current) {
		this.current = current;
	}

	public int getPages() {
		return pages;
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	public int getFirst() {
		return first;
	}

	public void setFirst(int first) {
		this.first = first;
	}

	public long getRows() {
		return rows;
	}

	public void setRows(long rows) {
		this.rows = rows;
	}

	public List<T> getDataList() {
		return dataList;
	}

	public void setDataList(List<T> dataList) {
		this.dataList = dataList;
	}

	public String getSearchHql() {
		return searchHql;
	}

	public void setSearchHql(String searchHql) {
		this.searchHql = searchHql;
	}

	public String getCountHql() {
		return countHql;
	}

	public void setCountHql(String countHql) {
		this.countHql = countHql;
	}

	public void calc() {
		pages = Integer.parseInt(String.valueOf(rows % length == 0 ? (rows / length) : (rows / length + 1)));
		current = current < 1 ? 1 : (current > pages ? pages : current);
		first = (current - 1) * length;
		
		//add start by liguolong 2014-04-30
		if(pages < displayPageCount){
			firstPageNum = 1;
			lastPageNum = pages;
		}else{
			firstPageNum = current - displayPageCount / 2;
			lastPageNum = current + displayPageCount / 2;
			if(firstPageNum < 1){
				firstPageNum = 1;
				lastPageNum = displayPageCount;
			}
			if(lastPageNum > pages){
				firstPageNum = pages - displayPageCount + 1;
				lastPageNum = pages;
			}
		}
		//add end
	}

	public Map<String,Object> getParams() {
		return params;
	}

	public void setParams(Map<String,Object> params) {
		this.params = params;
	}

	public int getFirstPageNum() {
		return firstPageNum;
	}

	public void setFirstPageNum(int firstPageNum) {
		this.firstPageNum = firstPageNum;
	}

	public int getLastPageNum() {
		return lastPageNum;
	}

	public void setLastPageNum(int lastPageNum) {
		this.lastPageNum = lastPageNum;
	}
	
}
